import Cookies from "js-cookie";
import request from "../index";
import { version } from "@/config";

// 加入购物车
export function createGoodsToCart({goodsId, goodsExtendId, projectId, price, amount, startDate, endDate}) {
  const userInfo = JSON.parse(Cookies.get('userInfo') || '{}');
  return request({
    url: `/ssop/api/goods-v${version}/goodsCart/save`,
    method: "post",
    data: {
      userId: userInfo?.id || "",
      goodsId, 
      goodsExtendId, 
      projectId, 
      price, 
      amount, 
      startDate, 
      endDate
    }
  })
}

// 删除购物车产品 ids:数组
export function removeGoodsFromCart(ids) {
  return request({
    url: `/ssop/api/goods-v${version}/goodsCart/delete`,
    method: "get",
    params: {
      ids
    }
  })
}

// 减/加购物车产品
export function reduceOrAddGoodsToCart({id, number}) {
  return request({
    url: `/ssop/api/goods-v${version}/goodsCart/updateNum`,
    method: "get",
    params: {
      id,
      number
    }
  })
}

// 查询购物车产品
export function getCartList(phone = "") {
  const userInfo = JSON.parse(Cookies.get('userInfo') || '{}');
  return request({
    url: `/ssop/api/goods-v${version}/goodsCart/list/activity`,
    method: "get",
    params: {
      userId: userInfo?.id || '',
      phone
    }
  })
}

// 提交购物车产品
export function commitCartList(data) {
  return request({
    url: `/ssop/api/goods-v${version}/goodsCart/list/activity/commit`,
    method: "post",
    data: {
      phone: '',
      ...data
    }
  })
}

// 提交购物车订单，支付
export function createCartOrder(data) {
  return request({
    url: `/ssop/api/order-v${version}/unified/order/saveCartActivity`,
    method: "post",
    data
  })
}

// H5 支付
export function orderCartPayH5(orderId) {
  return request({
    url: `/ssop/api/order-v${version}/order/cart/pay`,
    method: "POST",
    data: {
      orderId,
      orderClient: 5
    },
  });
}

// 获取支付订单状态
export function orderCartPayStatus(orderId) {
  return request({
    url: `/ssop/api/order-v${version}/unified/order/getCartPayStatus?mainId=${orderId}`,
    method: "get",
  });
}